//
//  MessageExtension.swift
//  Runner
//
//  Created by 123456 on 2023/9/25.
//

import Foundation
import Hyphenate
import HelpDesk



class MessageManage {
    
    
    
    
    static func messagesToListMap(messages: [Any]!) ->[Any]{
        
        
        var msgs:[Any] = [];
        messages.forEach { message in
            msgs.append(messageToMap(message: message as! HDMessage));
        }
        
        return msgs;
    }
    
    
    static func messageToMap(message: HDMessage) ->Dictionary<String, Any>{
        
        ///消息json
        var messageData = [String: Any]()
        messageData["direct"] = parseMessageDirect(direction: message.direction)
        messageData["from"] = message.from
        messageData["to"] = message.to
        messageData["sendTime"] = message.messageTime
        messageData["status"] = parseMessageStatus(status: message.status)
        messageData["msgId"] = message.messageId
        messageData["type"] = parseMessageType(body: message.body)
        messageData["body"] = messageBody(body: message.body);
        if(message.body is EMTextMessageBody){
            messageData ["ext"] = message.ext;
            messageData["extType"] = parseMessageExtType(message: message)
        }
        
        return messageData;
        
    }
    
    static func parseMessageStatus(status: HDMessageStatus) -> String {
        
        switch status {
        case HDMessageStatusPending:
            return "create"
        case HDMessageStatusDelivering:
            return "inProgress"
        case HDMessageStatusSuccessed:
            return "success"
        case HDMessageStatusFailed:
            return "fail"
        default:
            return ""
        }
    }
    
    static func parseMessageDirect(direction: HDMessageDirection) -> String {
        switch direction {
        case HDMessageDirectionSend:
            return "send"
        case HDMessageDirectionReceive:
            return "receive"
        default:
            return ""
        }
    }
    
    static func  parseMessageExtType(message: HDMessage) -> String {
        
//        HDExtGeneralMsg,  // 默认选项，一般为正常的文本消息
//        HDExtEvaluationMsg, // 满意度评价消息
//        HDExtOrderMsg, // 订单消息
//        HDExtTrackMsg, // 轨迹消息
//        HDExtFormMsg, // 表单消息
//        HDExtRobotMenuMsg, // 机器人菜单消息
//        HDExtArticleMsg, // 图文混排消息
//        HDExtToCustomServiceMsg, // 转人工客服消息
//        HDExtBigExpressionMsg, // 大表情消息
//        HDExtNeedScoreMsg // 机器人结尾消息(需要根据isNeedScore判断是否显示"评价/未评价")
        switch HDMessageHelper.getMessageExtType(message) {
            
        case HDExtEvaluationMsg:
            return "EvaluationMsg"
        case HDExtOrderMsg:
            return "OrderMsg"
        case HDExtTrackMsg:
            return "TrackMsg"
        case HDExtFormMsg:
            return "FormMsg"
        case HDExtRobotMenuMsg:
            return "RobotMenuMsg"
        case HDExtArticleMsg:
            return "ArticlesMsg"
        case HDExtToCustomServiceMsg:
            return "ToCustomServiceMsg"
        case HDExtBigExpressionMsg:
            return "BigExpressionMsg"
        case HDExtNeedScoreMsg:
            return "scoreMsg"
        case HDExtGeneralMsg:
            return "GeneralMsg"
         default:
            return ""
        }
    }
    
    static func  parseMessageType(body: EMMessageBody) -> String {
        
        ///文字消息
        if body is EMTextMessageBody {
            return "text";
        }
        ///视频消息
        else if body is EMVideoMessageBody {
            return "video";
        }
        ///图片消息
        else if body is EMImageMessageBody {
            return "image";
        }
        ///文件消息
        else if body is EMFileMessageBody {
            return "file";
        }
        ///自定义消息
        else if body is EMCustomMessageBody {
            return "custom";
        }
        ///（语音<VOICE>，
        else if body is EMVoiceMessageBody {
            return "voice";
        }
        ///命令<CMD>,
        else if body is EMCmdMessageBody {
            return "cmd";
        }
        ///位置<LOCATION>）
        else if body is EMLocationMessageBody {
            return "location";
        }
        
        return ""

        
    }
    
    
    
//
    ///消息body
    static func messageBody(body: EMMessageBody) -> Dictionary<String, Any>{
        
    
        var bodyMap = [String: Any]()
        //文字消息
        if body is EMTextMessageBody {
            let textMsg:EMTextMessageBody = body as! EMTextMessageBody
            bodyMap["message"] = textMsg.text
        }
        //视频消息
        else if body is EMVideoMessageBody {
            
            let videoMsg:EMVideoMessageBody = body as! EMVideoMessageBody
            
            bodyMap["thumbnailUrl"] = videoMsg.thumbnailRemotePath
            bodyMap["thumbnailWidth"] = videoMsg.thumbnailSize.width
            bodyMap["thumbnailHeight"] = videoMsg.thumbnailSize.height
            bodyMap["duration"] = videoMsg.duration
            bodyMap["videoFileLength"] = videoMsg.fileLength
            bodyMap["remoteUrl"] = videoMsg.remotePath
            bodyMap["localUrl"] = videoMsg.localPath
            bodyMap["displayName"] = videoMsg.displayName
            bodyMap["fileName"] = videoMsg.displayName
            
        }
        //图片消息
        else if body is EMImageMessageBody {
            let imageMsg:EMImageMessageBody = body as! EMImageMessageBody
            bodyMap["thumbnailUrl"] = imageMsg.thumbnailRemotePath
            bodyMap["thumbnailLocalPath"] = imageMsg.thumbnailLocalPath
            bodyMap["width"] = imageMsg.size.width
            bodyMap["height"] = imageMsg.size.height
            bodyMap["isSendOriginalImage"] = imageMsg.compressionRatio == 1 // 是否原图
            bodyMap["remoteUrl"] = imageMsg.remotePath
            bodyMap["localUrl"] = imageMsg.localPath
            bodyMap["displayName"] = imageMsg.thumbnailDisplayName
            bodyMap["fileName"] = imageMsg.displayName
            bodyMap["fileSize"] = imageMsg.fileLength
            
        }
        //文件消息
        else if body is EMFileMessageBody {
            
            let fileMsg:EMFileMessageBody = body as! EMFileMessageBody
            bodyMap["remoteUrl"] = fileMsg.remotePath
            bodyMap["localUrl"] = fileMsg.localPath
            bodyMap["displayName"] = fileMsg.displayName
            bodyMap["fileName"] = fileMsg.displayName
        }
        ///命令<CMD>,
        else if body is EMCmdMessageBody {
            let cmdMsg:EMCmdMessageBody = body as! EMCmdMessageBody
            bodyMap["isDeliverOnlineOnly"] = cmdMsg.isDeliverOnlineOnly // 是否只投递给在线用户
            bodyMap["action"] = cmdMsg.action
        }
        ///位置<LOCATION>）
        else if body is EMLocationMessageBody {
            let locationMsg:EMLocationMessageBody = body as! EMLocationMessageBody
            bodyMap["address"] = locationMsg.address
            bodyMap["latitude"] = locationMsg.latitude
            bodyMap["longitude"] = locationMsg.longitude
            bodyMap["buildingName"] = ""
        }
        ///（语音<VOICE>，
        else if body is EMVoiceMessageBody {
            let voiceMsg:EMVoiceMessageBody = body as! EMVoiceMessageBody
            bodyMap["length"] = voiceMsg.duration // 语音时间长度
            bodyMap["remoteUrl"] = voiceMsg.remotePath
            bodyMap["localUrl"] = voiceMsg.localPath
            bodyMap["displayName"] = voiceMsg.displayName
            bodyMap["fileName"] = voiceMsg.displayName
            bodyMap["fileSize"] = voiceMsg.fileLength
        }
        //自定义消息
        else if body is EMCustomMessageBody {
            
        }
        
        return bodyMap
 
    }

}
